$(function () {
    load(null);
});

function sousuo(){
    var sname=$("#sousuoa").val();
    load(null,sname);
}
function load(currentPage,sname){

    $.get("paging/product",{currentPageStr:currentPage,sname:sname},function (product){
        var lis = "";

        for (var i = 0; i < product.list.length; i++) {
            var route = product.list[i];
            var li = '<li>\n'+
                '<a href="jump/product_xq?id='+route.id+'" onclick="PClickadd('+route.id+')">\n'+
                '<img src="'+route.img+'" />\n'+
                '<p>'+route.sname+'</p>\n'+
                '</a>\n'+
                '</li>';
            lis+=li;
        }
        $("#productStr").html(lis);
        window.scroll(0,0);


        // </div>
        var liss = "";
        var fristPage = '<li onclick="javascript:load('+1+',\''+sname+'\')"><a href="javascript:void(0)">首页</a></li>'

        var beforeNum = product.currentPage - 1;
        if(beforeNum<=0){
            beforeNum=1;
        }
        var beforePage = '<li class="threeword" onclick="javascript:load('+beforeNum+',\''+sname+'\')"><a href="javascript:void(0)">上一页</a></li>';
        liss += fristPage;
        liss += beforePage;

        // 定义开始位置begin,结束位置 end
        var begin; // 开始位置
        var end ; //  结束位置


        //1.要显示10个页码
        if(product.totalPage < 10){
            //总页码不够10页
            begin = 1;
            end = product.totalPage;
        }else{
            //总页码超过10页

            begin = product.currentPage - 5 ;
            end = product.currentPage + 4 ;

            //2.如果前边不够5个，后边补齐10个
            if(begin < 1){
                begin = 1;
                end = begin + 9;
            }
            //3.如果后边不足4个，前边补齐10个
            if(end > product.totalPage){
                end = product.totalPage;
                begin = end - 9 ;
            }
        }

        for(var i=begin; i<=end;i++){
            var li;
            if(product.currentPage==i){
                li = '<li class="curPage" onclick="javascript:load('+i+',\''+sname+'\')"><a href="javascript:void(0)">'+i+'</a></li>'
            }else {
                li = '<li onclick="javascript:load('+i+',\''+sname+'\')"><a href="javascript:void(0)">'+i+'</a></li>'
            }

            liss += li;
        }


        var lastPage = '<li class="threeword" onclick="javascript:load('+product.totalPage+',\''+sname+'\')"><a href="javascript:void(0)">末页</a></li>';
        var afterNum = product.currentPage+1;
        if(afterNum>=product.totalPage){
            afterNum =product.totalPage;
        }
        var nextPage = '<li class="threeword" onclick="javascript:load('+afterNum+',\''+sname+'\')"><a href="javascript:void(0)">下一页</a></li>';

        liss += nextPage;
        liss += lastPage;

        //将lis内容设置到 ul
        $("#pageNum").html(liss);

    });

}